<template>
  <div class="app-wrapper">
    <header class="app-header">
      <div class="container header-inner">
        <div class="logo">
          <router-link to="/">智问社区</router-link>
        </div>
        
        <div class="nav-menu">
          <router-link to="/home" class="nav-item">首页</router-link>
          <router-link to="/questions" class="nav-item">问答</router-link>
          <a class="nav-item ask-btn" @click.prevent="openAskDialog">提问</a>
        </div>
        
        <div class="search-box">
          <SearchBar />
        </div>
        
        <div class="user-area">
          <template v-if="userStore.isLogin">
            <div class="notification-wrapper">
              <NotificationIcon />
            </div>
            <el-dropdown @command="handleCommand">
              <div class="user-info">
                <el-avatar :src="userStore.userInfo?.avatar" :size="32"></el-avatar>
                <span class="username">{{ userStore.userInfo?.nickname }}</span>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="notifications">我的通知</el-dropdown-item>
                  <el-dropdown-item command="settings">设置</el-dropdown-item>
                  <el-dropdown-item v-if="userStore.userInfo?.role === 3" command="admin">管理后台</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <el-button type="text" @click="$router.push('/login')">登录</el-button>
            <el-button type="primary" @click="$router.push('/register')">注册</el-button>
          </template>
        </div>
      </div>
    </header>
    
    <main class="app-main">
      <div class="container">
        <router-view />
      </div>
    </main>
    
    <footer class="app-footer">
      <div class="container">
        <p>© 2023 智问社区 - 专业问答平台</p>
      </div>
    </footer>

    <!-- 快速提问弹窗 -->
    <QuickAskDialog
      v-model:visible="askDialogVisible"
      @question-submitted="handleQuestionSubmitted"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useUserStore } from '../stores/user';
import SearchBar from '../components/SearchBar.vue';
import NotificationIcon from '../components/NotificationIcon.vue';


const router = useRouter();
const userStore = useUserStore();
const askDialogVisible = ref(false);

// 打开提问对话框
const openAskDialog = () => {
  // 检查用户是否登录
  if (!userStore.isLogin) {
    ElMessage.warning('请先登录后再提问');
    router.push('/login?redirect=/home');
    return;
  }
  
  askDialogVisible.value = true;
};

// 处理问题提交成功
const handleQuestionSubmitted = (question) => {
  if (question && question.id) {
    // 跳转到问题详情页
    router.push(`/questions/${question.id}`);
  }
};

// 下拉菜单命令处理
const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/my-profile');
      break;
    case 'settings':
      router.push('/settings');
      break;
    case 'notifications':
      router.push('/notifications');
      break;
    case 'admin':
      router.push('/admin');
      break;
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        
        await userStore.logoutAction();
        ElMessage.success('已退出登录');
        router.push('/home');
      } catch (error) {
        // 取消退出
      }
      break;
    default:
      break;
  }
};
</script>

<style scoped>
.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.app-header {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  display: flex;
  align-items: center;
  height: 64px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  margin-right: 40px;
}

.logo a {
  color: #0071e3;
  text-decoration: none;
}

.nav-menu {
  display: flex;
  margin-right: 20px;
}

.nav-item {
  padding: 0 15px;
  color: #333;
  text-decoration: none;
  line-height: 64px;
}

.nav-item:hover {
  color: #0071e3;
}

.ask-btn {
  color: #0071e3;
  font-weight: bold;
  cursor: pointer;
}

.search-box {
  flex: 1;
  max-width: 400px;
  margin: 0 20px;
}

.user-area {
  display: flex;
  align-items: center;
}

.notification-wrapper {
  margin-right: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  color: #333;
}

.app-main {
  flex: 1;
  padding: 20px 0;
  background-color: #f5f5f7;
}

.app-footer {
  background-color: #fff;
  padding: 20px 0;
  text-align: center;
  color: #666;
  border-top: 1px solid #eee;
}
</style> 